package com.tj.collection.db.dao.impl;

import com.fqgj.common.api.Page;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.StringUtils;
import com.tj.collection.db.dao.ClAgentsDao;
import com.tj.collection.db.entity.ClAgentsEntity;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2019-09-03
 * Time: 10:17
 */
@Repository
public class ClAgentsDaoImpl extends AbstractBaseMapper<ClAgentsEntity> implements ClAgentsDao {

    @Override
    public List<ClAgentsEntity> selectAgentListByParams(Map<String, Object> params) {
        if (!params.containsKey("deleted")) {
            params.put("deleted", 0);
        }
        return getSqlSession().selectList(this.getStatement("selectByParams"), params);
    }

    public Integer countAgent(Map<String, Object> params) {
        if (!params.containsKey("deleted")) {
            params.put("deleted", 0);
        }
        return getSqlSession().selectOne(this.getStatement("queryTotal"), params);
    }

    @Override
    public List<ClAgentsEntity> selectAgentListByMerchantCode(String merchantCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("merchantCode", merchantCode);
        return this.selectAgentListByParams(params);
    }

    @Override
    public List<ClAgentsEntity> selectAgentListByMerchantCode(String merchantCode, String stageCode, Page page) {
        Map<String, Object> params = new HashMap<>();
        params.put("merchantCode", merchantCode);
        if (StringUtils.isNotEmpty(stageCode)) {
            params.put("stageCode", stageCode);
        }
        if (page != null) {
            params.put("offset", page.getStartIndex());
            params.put("limit", page.getEndIndex());
            page.setTotalCount(countAgent(params));
        }
        return this.selectAgentListByParams(params);
    }

    @Override
    public List<ClAgentsEntity> getActiveListByMerchantCode(String merchantCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("merchantCode", merchantCode);
        return getSqlSession().selectList(this.getStatement("getActiveListByMerchantCode"), map);
    }

    @Override
    public List<ClAgentsEntity> getActiveListByMerchantCode(String merchantCode, String stageCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("merchantCode", merchantCode);
        map.put("stageCode", stageCode);
        return getSqlSession().selectList(this.getStatement("getActiveListByMerchantCode"), map);
    }

    @Override
    public ClAgentsEntity getByAdminId(Integer adminId) {
        Map<String, Object> map = new HashMap<>();
        map.put("adminId", adminId);
        return getSqlSession().selectOne(this.getStatement("getByAdminId"), map);
    }

    @Override
    public ClAgentsEntity selectAgentByAdminIdAndStage(Integer adminId, String stageCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("adminId", adminId);
        params.put("stageCode", stageCode);
        List<ClAgentsEntity> clAgentsEntityList = this.selectAgentListByParams(params);
        if (clAgentsEntityList != null && clAgentsEntityList.size() > 0) {
            return clAgentsEntityList.get(0);
        }
        return null;
    }

    @Override
    public int deleteById(Integer adminId) {
        return getSqlSession().delete(this.getStatement("deleteById"), adminId);
    }
}
